local s15__zhongyi = fk.CreateSkill {
  name = "s15__zhongyi",
  -- tags = { Skill.Limited },
}

Fk:loadTranslationTable {
  ["s15__zhongyi"] = "忠义",
  [":s15__zhongyi"] = "限定技，出牌阶段，你可以选择你的等同于己方存活角色数张牌，置于每名己方角色武将上，然后你摸等量张牌。武将牌上有\"忠义\"牌的角色使用【杀】造成伤害时，此伤害+1。此伤害+1的【杀】结算结束后，该角色的\"忠义\"牌置入弃牌堆。",
  ["#s15__zhongyi"] = "忠义：选择你的等同于己方存活角色数张牌，置于每名己方角色武将上",
}

-- 限定技主体效果
s15__zhongyi:addEffect("active", {
  anim_type = "support",
  target_num = 0,
  min_card_num = 1,
  prompt = "#s15__zhongyi",
  derived_piles = s15__zhongyi.name,
  can_use = function(self, player)
    if player:usedSkillTimes(self.name, Player.HistoryGame) > 0 then return false end

    local room = Fk:currentRoom()
    local allies = {}
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) then
        table.insert(allies, p)
      end
    end

    return #allies > 0 and #player:getCardIds("he") >= #allies
  end,
  card_filter = function(self, player, to_select, selected)
    local room = Fk:currentRoom()
    local allies = {}
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) then
        table.insert(allies, p)
      end
    end
    return #selected < #allies
  end,

  on_use = function(self, room, effect)
    local player = effect.from
    local allies = {}
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) then
        table.insert(allies, p)
      end
    end

    -- 为每名己方角色放置一张忠义牌
    for i, ally in ipairs(allies) do
      if i <= #effect.cards then
        local card = effect.cards[i]
        ally:addToPile(s15__zhongyi.name, { card }, true, s15__zhongyi.name, player)
      end
    end

    -- 摸等量的牌
    player:drawCards(#allies, self.name)
  end,
})

-- 使用【杀】无视目标防具
s15__zhongyi:addEffect("invalidity", {
  invalidity_func = function(self, player, skill)
    if skill:getSkeleton().attached_equip and Fk:cloneCard(skill:getSkeleton().attached_equip).sub_type == Card.SubtypeArmor then
      if RoomInstance then
        local logic = RoomInstance.logic
        local event = logic:getCurrentEvent()
        local from = nil
        repeat
          if event.event == GameEvent.UseCard then
            local use = event.data
            if use.card.trueName == "slash" and table.contains(use.tos, player.id) then
              from = use.from
              break
            end
          elseif event.event == GameEvent.Damage then
            local damage = event.data
            if damage.to == player then
              from = damage.from
              break
            end
          end
          event = event.parent
        until event == nil
        if from and #from:getPile(s15__zhongyi.name) > 0 then
          return true
        end
      end
    end
  end,
})

-- 使用【杀】造成伤害时，伤害+1
s15__zhongyi:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and
        #player:getPile(s15__zhongyi.name) > 0 and
        data.card and
        data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("s15__zhongyi")
    room:notifySkillInvoked(player, "s15__zhongyi", "offensive")

    -- 伤害+1
    data:changeDamage(1)
  end,
})

-- 【杀】结算结束后移除忠义牌
s15__zhongyi:addEffect(fk.CardUseFinished, {
  anim_type = "support",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    -- target是使用【杀】的角色，检查该角色是否有忠义牌且此杀造成过伤害
    return not target.dead and
        data.card and
        data.card.trueName == "slash" and
        #target:getPile(s15__zhongyi.name) > 0 and
        data.damageDealt
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local zhongyiCards = target:getPile(s15__zhongyi.name)
    if #zhongyiCards > 0 then
      room:moveCardTo(zhongyiCards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile,
        s15__zhongyi.name, nil, true, target.id)
    end
  end,
})

return s15__zhongyi
